package com.nstar.base.util
{
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Stage;
	import flash.display.StageDisplayState;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.net.SharedObject;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	
	import mx.core.Container;
	
	public class SystemUtils
	{
		//全屏模式与正常模式的切换
		//需要修改html-template目录下的index.template.html文件中的几个参数，如下
		//1.."allowScriptAccess","sameDomain" -> "allowFullScreen","true"
		//2.."allowScriptAccess","sameDomain" -> "allowFullScreen","true"
		//3..allowScriptAccess="sameDomain"	  -> "allowFullScreen"="true"
		//
		public static function toggleDisplayState(stage:Stage):void{
			stage.displayState = (stage.displayState == 
				StageDisplayState.NORMAL?StageDisplayState.FULL_SCREEN:StageDisplayState.NORMAL);
		}
		
		//当前时间与基准时间的比较结果，用于计时
		public static function calcInterval(benchmark:Date):Number{
			var now:Date = new Date();
			return now.getTime() - benchmark.getTime();
		}
		
		//保存数据到本地
		public static function setMemory(url:String,property:String,value:String):void{
			var so:SharedObject = SharedObject.getLocal(url,"/");
			so.data[property] = value;
			
			so.flush();
		}
		//从本地获取数据
		public static function getMemory(url:String, property:String):String{
			var so:SharedObject = SharedObject.getLocal(url,"/");
			return so.data[property];
		}
		
		//保存列表数据到本地
		public static function setMemoryList(url:String,propList:Array,valueList:Array):void{
			var so:SharedObject = SharedObject.getLocal(url);
			for(var i:int = 0;i<propList.length;i++){
				so.data[propList[i]] = valueList[i];
			}
			
			so.flush();
		}
		
		//取得本地列表数据
		public static function getMemoryList(url:String,propList:Array):Array{
			var ary:Array = new Array();
			var so:SharedObject = SharedObject.getLocal(url);
			for(var i:int = 0;i<propList.length;i++){
				ary.push(so.data[propList[i]]);
			}
			
			return ary;
		}
		
		//显示保存在url中的shareobject对象值
		public static function showMemory(url:String):String{
			var so:SharedObject = SharedObject.getLocal(url);
			var info:String = "";
			/* for (var item:Object in so){
				info +=item+"="+so[item]+"\n"; 
				trace(info);
			}	 */
			return info;		
		}
		
		//转到指定URL
		public static function forwardTo(url:String):void{
			navigateToURL(new URLRequest(url));
		}
		
		//产生指定范围内的随机数
		public static function randRange(min:Number, max:Number):Number {
		    var randomNum:Number = Math.floor(Math.random() * (max - min + 1)) + min;
		    return randomNum;
		}
		
	    //在Canvas中使用位图绘制背景,填充方式有平铺、居中、拉伸、默认背景透明色为白色
	    public static function drawBackground(source:DisplayObject,
	    	dest:Container,flag:String="TILE",
	    	transparent:Boolean = true, fillColor:uint = 0xFFFFFF):void{
	    	dest.graphics.clear();
	    	var bd:BitmapData = new BitmapData(source.width,source.height,true,0xFFFFFF);
	    	bd.draw(source,new Matrix());
	    	var matrix:Matrix = new Matrix();//用于对目标位图进行的转换操作
	    	switch(flag){
	    		case "TILE":
	    			dest.graphics.beginBitmapFill(bd,matrix);
	    			dest.graphics.drawRect(0,0,dest.width,dest.height);
	    			break;
	    		case "CENTER":
			    	var p:Point = new Point();
			    	p.x = (dest.width-source.width)/2;
			    	p.y = (dest.height - source.height)/2;	  
			    	matrix.translate(p.x,p.y);  		
	    			dest.graphics.beginBitmapFill(bd,matrix);
	    			dest.graphics.drawRect(p.x,p.y,source.width,source.height);
	    			break;
	    		case "STRETCH":
	    			matrix.scale(dest.width/source.width,dest.height/source.height);
			    	dest.graphics.beginBitmapFill(bd,matrix,false);
			    	dest.graphics.drawRect(0,0,dest.width,dest.height);	    			
	    			break;
	    	}
	    	dest.graphics.endFill();
	    }
		
	}
}